/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.service.base.processor.post;

import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.common.core.result.ListResult;
import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.common.core.result.SingleResult;
import info.peigen.hotpot.common.service.base.event.LogEventHandler;
import info.peigen.hotpot.common.service.base.event.ServiceResultLogEvent;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

/**
 * <b>(PostLogEventProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/26
 */
@SuppressWarnings({"unused", "rawtypes"})
@Component
public class PostLogEventProcessor implements Processor {

    @Inject
    protected LogEventHandler logEventHandler;

    @Override
    public Integer level() {
        return 100;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        // 打印[结果]日志
        ServiceResultLogEvent resultLogEvent = new ServiceResultLogEvent();
        if (ObjectUtil.isNotNull(serviceContext.getOrder())) {
            resultLogEvent = ServiceResultLogEvent.builder()
                    .partnerId(serviceContext.getOrder().getPartnerId())
                    .productCode(serviceContext.getOrder().getProductCode())
                    .bizOrderNo(serviceContext.getOrder().getBizOrderNo())
                    .build();
        }

        // 1 组装返回对象
        // 1.1 组装无返回对象结果
        if (serviceContext.getResult() != null) {
            resultLogEvent.setResultBase(serviceContext.getResult());
        }
        // 1.2 组装单个返回对象
        if (serviceContext.getResult() instanceof SingleResult) {
            resultLogEvent.setSingleResult((SingleResult<?>) serviceContext.getResult());
        }
        // 1.3 组装集合返回对象
        if (serviceContext.getResult() instanceof ListResult) {
            resultLogEvent.setListResult((ListResult<?>) serviceContext.getResult());
        }
        // 1.4 组装分页返回对象
        if (serviceContext.getResult() instanceof PageResult) {
            resultLogEvent.setPageResult((PageResult<?>) serviceContext.getResult());
        }

        logEventHandler.eventPost(resultLogEvent);
    }

}
